<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h2>欢迎回来</h2>
        <p>登录您的账号继续使用</p>
      </div>
      
      <form class="login-form" @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="email">电子邮箱</label>
          <input
            id="email"
            type="email"
            v-model="email"
            placeholder="请输入您的电子邮箱"
            required
            class="form-input"
          />
          <div v-if="errors.email" class="error-message">{{ errors.email }}</div>
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <div class="password-input-wrapper">
            <input
              id="password"
              :type="showPassword ? 'text' : 'password'"
              v-model="password"
              placeholder="请输入您的密码"
              required
              class="form-input password-input"
            />
            <button
              type="button"
              class="toggle-password"
              @click="togglePasswordVisibility"
              aria-label="切换密码可见性"
            >
              <svg v-if="!showPassword" width="18" height="18" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z" fill="currentColor"/>
              </svg>
              <svg v-else width="18" height="18" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M12 7c2.76 0 5 2.24 5 5 0 .65-.13 1.26-.36 1.83l2.92 2.92c1.51-1.26 2.7-2.89 3.43-4.75-1.73-4.39-6-7.5-11-7.5-1.4 0-2.74.25-3.98.7l2.16 2.16C10.74 7.13 11.35 7 12 7zM2 4.27l2.28 2.28.46.46C3.08 8.3 1.78 10.02 1 12c1.73 4.39 6 7.5 11 7.5 1.55 0 3.03-.3 4.38-.84l.42.42L19.73 22 21 = 20.73 3.27 3 2 4.27zM7.53 9.8l1.55 1.55c-.05.21-.08.43-.08.65 0 1.66 1.34 3 3 3 .22 0 .44-.03.65-.08l1.55 1.55c-.67.33-1.41.53-2.2.53-2.76 0-5-2.24-5-5 0-.79.2-1.53.53-2.2zm4.31-.78l3.15 3.15.02-.16c0-1.66-1.34-3-3-3l-.17.01z" fill="currentColor"/>
              </svg>
            </button>
          </div>
          <div v-if="errors.password" class="error-message">{{ errors.password }}</div>
        </div>
        
        <div class="form-options">
          <label class="remember-checkbox">
            <input type="checkbox" v-model="rememberMe" />
            <span>记住我</span>
          </label>
          <a href="#" class="forgot-password">忘记密码？</a>
        </div>
        
        <button type="submit" class="login-button" :disabled="isSubmitting">
          {{ isSubmitting ? '登录中...' : '登录' }}
        </button>
        
        <button type="button" class="login-button test-login-btn" @click="testLogin">
          测试登录（管理员）
        </button>
        
        <div class="register-link-container">
          <span>还没有账号？</span>
          <router-link to="/register" class="register-link">立即注册</router-link>
        </div>
        
        <div class="divider">
          <span>或者</span>
        </div>
        
        <div class="social-login">
          <button type="button" class="social-button google">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M22.54 6.42a2.78 2.78 0 0 0-1.94-2C18.88 4 12 4 12 4s-6.88 0-8.6.46a2.78 2.78 0 0 0-1.94 2A29 29 0 0 0 1 11.75a29 29 0 0 0 .46 5.33A2.78 2.78 0 0 0 3.4 19c1.72.46 8.6.46 8.6.46s6.88 0 8.6-.46a2.78 2.78 0 0 0 1.94-2 29 29 0 0 0 .46-5.25 29 29 0 0 0-.46-5.33z" fill="#4285F4"/>
              <path d="M12 15.5c-1.54 0-2.82-.8-3.5-1.8v4.3h6.97v-4.3c-.68 1-1.96 1.8-3.47 1.8z" fill="#34A853"/>
              <path d="M8.5 11.1v-1.2h7v1.2H8.5z" fill="#FBBC05"/>
              <path d="M12 8.8c1.18 0 2.22.4 2.94 1.17l1.87-1.86C15.87 6.5 13.98 6 12 6c-2.12 0-4 1-5.45 2.4l1.87 1.86C9.78 9.2 10.82 8.8 12 8.8z" fill="#EA4335"/>
              <path d="M1 11.75a29 29 0 0 0 .46 5.33 2.78 2.78 0 0 0 1.94 2c1.72.46 8.6.46 8.6.46s6.88 0 8.6-.46a2.78 2.78 0 0 0 1.94-2 29 29 0 0 0 .46-5.33 29 29 0 0 0-.46-5.25 2.78 2.78 0 0 0-1.94-2C18.88 4 12 4 12 4s-6.88 0-8.6.46a2.78 2.78 0 0 0-1.94 2A29 29 0 0 0 1 11.75z" fill="none" stroke="#000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <span>使用谷歌账号登录</span>
          </button>
          <button type="button" class="social-button wechat">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M18.2 7.2c.6-1.1.9-2.4.9-3.8C19.1 1.5 17.6 0 15.7 0h-7.5C6.4 0 4.9 1.5 4.9 3.4c0 1.4.3 2.7.9 3.8-.4.1-.8.2-1.2.4-1.6 1-2.5 2.9-2.5 5 0 2.1.9 4 2.5 5 .4.2.8.3 1.2.4 0 .8.1 1.6.2 2.3-.2.1-.5.1-.7.1h-.7c-.5 0-.9.4-.9.9s.4.9.9.9h.7c.2 0 .5 0 .7-.1.4.8 1 1.6 1.8 2.2.9.6 2 1 3 1h5.3c1 0 2.1-.4 3-1 .8-.6 1.4-1.4 1.8-2.2.2.1.5.1.7.1h.7c.5 0 .9-.4.9-.9s-.4-.9-.9-.9h-.7c-.2 0-.5 0-.7.1.1-.8.2-1.6.2-2.3.4-.1.8-.2 1.2-.4 1.6-1 2.5-2.9 2.5-5 0-2.1-.9-4-2.5-5-.4-.2-.8-.3-1.2-.4zM7.4 16.6c-1.3 0-2.4-1.1-2.4-2.4 0-1.3 1.1-2.4 2.4-2.4s2.4 1.1 2.4 2.4c0 1.3-1.1 2.4-2.4 2.4zm9.2 0c-1.3 0-2.4-1.1-2.4-2.4 0-1.3 1.1-2.4 2.4-2.4s2.4 1.1 2.4 2.4c0 1.3-1.1 2.4-2.4 2.4z" fill="#07C160"/>
            </svg>
            <span>使用微信账号登录</span>
          </button>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { login as loginUser, saveRedirectUrl } from '../services/membershipService.js'

const router = useRouter()
const route = useRoute()
const email = ref('')
const password = ref('')
const rememberMe = ref(false)
const showPassword = ref(false)
const isSubmitting = ref(false)
const errors = ref({
  email: '',
  password: ''
})

// 切换密码可见性
const togglePasswordVisibility = () => {
  showPassword.value = !showPassword.value
}

// 验证表单
const validateForm = () => {
  let isValid = true
  
  // 重置错误信息
  errors.value = {
    email: '',
    password: ''
  }
  
  // 验证邮箱
  if (!email.value.trim()) {
    errors.value.email = '邮箱不能为空'
    isValid = false
  } else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email.value)) {
    errors.value.email = '请输入有效的邮箱地址'
    isValid = false
  }
  
  // 验证密码
  if (!password.value) {
    errors.value.password = '密码不能为空'
    isValid = false
  }
  
  return isValid
}

// 处理登录
const handleLogin = async () => {
  // 验证表单
  if (!validateForm()) {
    return
  }
  
  try {
    isSubmitting.value = true
    
    // 调用登录服务验证用户
    const result = loginUser(email.value, password.value, rememberMe.value)
    
    if (result.success) {
      // 根据返回的redirectUrl进行页面跳转
      router.push(result.redirectUrl)
    } else {
      // 登录失败，显示错误信息
      errors.value.email = result.error || '邮箱或密码错误'
    }
  } catch (error) {
    console.error('登录时发生错误:', error)
    errors.value.email = '登录时发生错误，请稍后再试'
  } finally {
    isSubmitting.value = false
  }
}

// 页面加载时，如果有redirect参数，保存它
if (route.query.redirect) {
  saveRedirectUrl(route.query.redirect)
}

// 测试登录功能
const testLogin = () => {
  // 测试登录，模拟管理员账号
  const adminUser = {
    id: 1,
    name: '管理员',
    email: 'admin@example.com',
    role: 'admin'
  }
  localStorage.setItem('user', JSON.stringify(adminUser))
  alert('管理员测试登录成功！')
  router.push('/admin')
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: var(--color-background-soft);
  padding: 2rem;
}

.login-card {
  background: var(--vt-c-white);
  border-radius: 12px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05), 0 1px 3px rgba(0, 0, 0, 0.1);
  padding: 2.5rem;
  width: 100%;
  max-width: 450px;
  transition: transform 0.3s ease;
}

.login-card:hover {
  transform: translateY(-5px);
}

.login-header {
  text-align: center;
  margin-bottom: 2rem;
}

.login-header h2 {
  font-size: 1.8rem;
  font-weight: 600;
  color: var(--color-heading);
  margin-bottom: 0.5rem;
}

.login-header p {
  color: var(--color-text);
  opacity: 0.8;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.password-input-wrapper {
  position: relative;
}

.form-input {
  padding: 0.75rem 1rem;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  font-size: 1rem;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.password-input {
  padding-right: 2.5rem;
}

.form-input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.1);
}

.toggle-password {
  position: absolute;
  right: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  color: var(--color-text);
  opacity: 0.7;
  transition: opacity 0.3s ease;
}

.toggle-password:hover {
  opacity: 1;
}

.error-message {
  color: var(--color-error);
  font-size: 0.875rem;
  margin-top: 0.25rem;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.remember-checkbox {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
}

.forgot-password {
  color: var(--color-primary);
  text-decoration: none;
  font-size: 0.875rem;
}

.forgot-password:hover {
  text-decoration: underline;
}

.login-button {
  padding: 0.75rem;
  background-color: var(--color-primary);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.login-button:hover:not(:disabled) {
  background-color: var(--color-primary-dark);
}

.login-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.test-login-btn {
  background-color: var(--color-secondary, #6c757d);
  margin-top: 1rem;
}

.test-login-btn:hover {
  background-color: var(--color-secondary-dark, #5a6268);
}

.register-link-container {
  text-align: center;
  margin-top: 1rem;
}

.register-link {
  color: var(--color-primary);
  text-decoration: none;
  font-weight: 500;
}

.register-link:hover {
  text-decoration: underline;
}

.divider {
  position: relative;
  height: 1px;
  background-color: var(--color-border);
  margin: 2rem 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.divider span {
  background-color: var(--vt-c-white);
  padding: 0 1rem;
  color: var(--color-text);
  font-size: 0.875rem;
}

.social-login {
  display: flex;
  gap: 1rem;
}

.social-button {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.75rem;
  background-color: transparent;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.social-button:hover {
  background-color: var(--color-background-soft);
  border-color: var(--color-primary);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    padding: 1rem;
  }
  
  .login-card {
    padding: 1.5rem;
  }
  
  .social-login {
    flex-direction: column;
  }
}
</style>